package com.firmiana.jt809.protocol.receive;

import com.firmiana.jt809.config.GlobalConfig;
import com.firmiana.jt809.entity.JT809Message;
import com.firmiana.jt809.parser.MessageParser;
import com.firmiana.jt809.protocol.IReceiveProtocol;
import com.firmiana.jt809.util.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 主链路车辆动态信息交换
 * 不需要返回给下级平台响应
 * 下级平台 --> 上级平台
 *
 * @author hucl
 * @date 2020/8/28 16:41
 */
@Slf4j
@Component
public class MainLinkCarInfoExgMsgProtocol implements IReceiveProtocol {

    @Override
    public String handle(JT809Message message) {
        String messageBody = message.getMessageBody();
        MessageParser msgParser = new MessageParser(messageBody);
        // 车牌号
        message.setPlateNo(msgParser.getString(21));
        // 车颜色
        message.setPlateColor(msgParser.getInt(1));
        // 子业务类型[用于后续步骤处理]
        message.setSubType(msgParser.getInt(2));
        // 后续数据长度[暂不知用啥用]
        message.setContentLength(msgParser.getInt(4));
        log.info("【主链路车辆动态信息交换】接收到消息, 业务类型:{}, 车牌号:{}", Integer.toHexString(message.getSubType()), message.getPlateNo());

        Class<? extends IReceiveProtocol> handleClazz = GlobalConfig.getReceiveProtocolByProtocolId(message.getSubType());
        if (handleClazz != null) {
            IReceiveProtocol protocol = SpringUtils.getBean(handleClazz);
            if (protocol == null) {
                log.warn("【主链路车辆动态信息交换】没找到对应的业务类型处理类, 业务类型:[{}]", message.getSubType());
                return null;
            }
            protocol.handle(message);
        }
        return null;
    }
}
